package com.bos.controller;

import com.bos.data.model.BosCommentModel;
import com.bos.service.CommentService;
import com.bos.service.LogsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/*
 *
 * 对评论表进行操作
 *
 * */
@Controller
public class CommentController {
    //int id=0;

    //1.查询出所有的评论信息
    @Autowired
    @Qualifier("commentServiceImpl")
    CommentService commentService;

    @Autowired
    private LogsService logsService;

    @RequestMapping(value = "/findComment")
    public String findComment(@RequestParam("cid") int id, HttpSession session, Model model) {
        //查询出所有的评论信息（如果根据项目id能查询到对应的评论信息的话）
        List<BosCommentModel> comms = commentService.findComment(id);
        model.addAttribute("comm", comms);
        //如果不能根据项目id查询到对应的评论信息的话，将项目id传给页面（当作comid）
        session.setAttribute("xid", id);
        return "jsp/comment";
    }

    //2.得到富文本中的值，将内容插入到评论表中
    @RequestMapping(value = "/insertComment", method = RequestMethod.POST)
    public ModelAndView insertComment(@RequestParam("content") String content, @RequestParam("sid") int comid, @RequestParam("cname") String cname,@RequestParam("image") String image, RedirectAttributes model, HttpSession session) {

        //得到当前系统时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        BosCommentModel comm = new BosCommentModel();
        //目前这个评论应从用户表中来
        comm.setCname(cname);
        comm.setCcontext(content);
        comm.setCtime(new Timestamp(new Date().getTime()));
        //关联的这个id应自定义
        comm.setComid(comid);
        comm.setImage(image);
        int row = commentService.insertComment(comm);
        //插入日志
        logsService.insertLogs(session, "添加评论");
        return new ModelAndView("redirect:/findComment?cid=" + comid);
    }

}
